package com.jn.util;


import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.core.io.ClassPathResource;

import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

@Component(value="mailSender")
public class MailSender {

	@Autowired
	public JavaMailSender javaMailSender = null;

	public MailSender() {
	}

	public void send(PostVO post) {

		JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
		MimeMessage mailMessage = senderImpl.createMimeMessage();
		// 设置utf-8或GBK编码，否则邮件会有乱码
		MimeMessageHelper messageHelper = null;
		try {
			messageHelper = new MimeMessageHelper(mailMessage, true, "utf-8");
			messageHelper.setTo(post.to);// 接受者
			messageHelper.setFrom(post.from);// 发送者
			messageHelper.setSubject(post.subject);// 主题			
			messageHelper.setText(post.Text,true);
			messageHelper.setBcc("cuiyl@hollycrm.com");
			// 这里的logo要与cid:logo一致,不然无法显示图片
/*			messageHelper.addInline("logo", new ClassPathResource(
					"com/holly/util/baidu.gif"));*/
			javaMailSender.send(mailMessage);
			System.out.println("邮件发送成功");
		} catch (Exception e) {

			e.printStackTrace();

		}

	}

}
